<template>
  <a-modal width="600px" v-model:visible="visible" title="新增转链白名单" @before-ok="onOk">
    <a-form ref="formRef" :model="form" size="medium" auto-label-width>
      <a-form-item field="platformId" label="所属平台">
        <a-select v-model="form.platformId" placeholder="选择所属平台">
          <a-option v-for="item in platformList" :key="item.id" :value="item.id" :label="item.title"></a-option>
        </a-select>
      </a-form-item>
      <a-form-item field="convertUrl" label="长链接">
        <a-textarea
          :auto-size="{
            minRows: 6,
            maxRows: 10
          }"
          v-model="form.convertUrl"
          placeholder="请输入长链接"
        />
      </a-form-item>
      <a-form-item field="shortUrl" label="短连接">
        <a-input v-model="form.shortUrl" placeholder="请输入短连接" />
      </a-form-item>
      <a-form-item label="到期时间" field="expiresAt">
        <a-date-picker show-time v-model="form.expiresAt" :time-picker-props="{ defaultValue: '23:59:59' }" />
      </a-form-item>
      <a-form-item field="desc" label="描述">
        <a-textarea v-model="form.desc" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script setup>
import { convertUrlWhiteListAdd } from '@/apis'
import { Message } from '@arco-design/web-vue'
import { reAssign } from '@/utils/common'

defineProps(['platformList'])
const emit = defineEmits(['getTableData'])

const visible = ref(false)
const formRef = ref()

const form = reactive({
  platformId: undefined,
  convertUrl: undefined,
  shortUrl: undefined,
  expiresAt: undefined,
  desc: undefined
})

// 打开编辑框并回显表单值
const addOrEdit = async () => {
  visible.value = true
  reAssign(form, false)
}

// 点击确认添加/编辑
const onOk = async () => {
  const validateResult = await formRef.value?.validate()
  if (validateResult) return false
  await convertUrlWhiteListAdd(form)
  Message.success('操作成功')
  emit('getTableData')
  return true
}

provide('formRef', formRef)
defineExpose({ addOrEdit })
</script>
